import { Method } from '../types/types';

export const parseAbi = (abiString: string): Method[] => {
    try {
        const abi = JSON.parse(abiString);
        if (!Array.isArray(abi)) {
            throw new Error('ABI must be an array');
        }

        return abi.filter(item => item.type === 'function').map(item => ({
            name: item.name,
            inputs: item.inputs || [],
            outputs: item.outputs || [],
            stateMutability: item.stateMutability,
            type: item.type
        }));
    } catch (error) {
        throw new Error(`Invalid ABI: ${error instanceof Error ? error.message : String(error)}`);
    }
};

export const formatMethodSignature = (method: Method): string => {
    const params = method.inputs.map(input => `${input.type} ${input.name}`).join(', ');
    return `${method.name}(${params})`;
};